﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taobao.common;

namespace Taobao.DBServer
{
    public partial class ShopDAL:DAL
    {

        public ShopDAL()
        #region >><<
        {
            dicDAL = new Dictionary<string, string>();
            dicDAL.Add("Table", "shop");
            dicDAL.Add("Field_1", "id,Name,Note,Credit,Level,LastUpdate,TimeStp,MemberID");
            dicDAL.Add("Field_2", "id,Name,Note,Credit,Level,LastUpdate,MemberID");
            dicDAL.Add("Field_2_values", "'{0}','{1}','{2}','{3}','{4}','{5}','{6}'");

            dicDAL.Add("Id", "id");
            dicDAL.Add("Name", "Name");

            dicDAL.Add("Update", "update " + dicDAL["Table"] + " set {1} = '{2}' where " + dicDAL["Id"] + " = {0}");
        }

        public bool isDispose { get; protected set; }

        public void Dispose()
        {

        }
        #endregion


        protected Dictionary<string, string> dicDAL = new Dictionary<string, string>();

        public bool AddNew(SHOP item)
        #region AddNew
        {
            bool rtn = true;

            //sql="insert into job (jobid,jobname) values ('AAA','bbb')"
           // dicDAL.Add("Field_2", "id,Name,Note,Credit,Level,LastUpdate,MemberID");
            //
            string SQL = "insert into " + dicDAL["table"] + " (" + dicDAL["Field_2"] + ") values (" + dicDAL["Field_2_values"] + ")";
            if (rtn)
            {
                SQL = string.Format(SQL, item.ID,
                                         item.Name,
                                         item.Note,
                                         item.Credit,
                                         item.Level,
                                         item.LastUpdate,
                                         item.MemberID);

                DBServer server = null;
                try
                {
                    server = new DBServer();
                    server.Open();
                    rtn = server.ExecSql(SQL);
                }
                catch (Exception ex)
                {
                    rtn = false;
                    OnError(ex);
                }
                finally
                {
                    if (server != null)
                        server.Close(); ;
                }
            }
            return rtn;
        }
        #endregion

        public bool GetData(long id, out SHOP item)
        #region >><<
        {
            item = null;

            string sql = "Select {1} from {0} where {2} = {3}";
            sql = string.Format(sql, dicDAL["Table"], dicDAL["Field_1"], dicDAL["Id"], id);

            return getData(sql, out item);
        }
        #endregion

        public bool Update(SHOP item)
        #region >><<
        {
            string sql = "update " + dicDAL["Table"] + " set {1} where " + dicDAL["Id"] + " = {0} " +
                         " ;select " + dicDAL["Field_1"] + " from " + dicDAL["Table"] + " where " + dicDAL["Id"] + " = {0} ";
            string sql2 = string.Format("{0} = '{1}',", dicDAL["Name"], item.Name) +
                          string.Format("{0} = '{1}',", dicDAL["Note"], item.Note) +
                          string.Format("{0} = '{1}',", dicDAL["Credit"], item.Credit) +
                          string.Format("{0} = '{1}',", dicDAL["Level"], item.Level) +
                          string.Format("{0} = '{1}',", dicDAL["MemberID"], item.MemberID) +
                          string.Format("{0} = '{1}' ", dicDAL["LastUpdate"], item.LastUpdate.ToString("yyyy-MM-DD HH:mm:ss"));

            sql = string.Format(sql, item.ID, sql2);
            DBServer server = null;
            string bsuccess = null;
            try
            {
                server = new DBServer();
                server.Open();

                bsuccess = server.ReturnExecSql(sql);
            }
            catch (Exception ex) { throw new DBException("fail", ex); }
            finally
            {
                if (server != null)
                    server.Close();
            }

            return !string.IsNullOrWhiteSpace(bsuccess);
        }
        #endregion


    }
}
